<template>
  <div id="table">
    <el-row>
      <el-col :span="24">
        <div class="table-grid-content">
          <el-button
            type="primary"
            @click="addRow()"
          >新增</el-button>
        </div>
      </el-col>
    </el-row>
 
    <!-- 表格 -->
    <div class="Table-table">
      <el-table
        id="table"
        :data="tableform"
        ref="tableform"
        height="500"
        border
      >
        <el-table-column
          type="selection"
          width="55"
        >
        </el-table-column>
        <el-table-column
          prop="name"
          label="姓名"
          width="400"
        >
        </el-table-column>
        <el-table-column
          prop="relationship"
          label="当前对应关系"
          width="400"
        >
        </el-table-column>
        <el-table-column
          prop="opp"
          label="操作"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index,tableform )"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
 
    <!-- 编辑窗口 -->
    <el-dialog
      title="编辑"
      :visible.sync="editFormVisible"
    >
      <el-form
        :model="editForm"
        ref="editForm"
      >
        <el-form-item
          label="姓名"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="editForm.name"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="当前对应关系"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="editForm.relationship"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div>
        <el-button @click="cancel()">取消</el-button>
        <el-button
          type="primary"
          v-on:click="sumbitEditRow()"
          :loading="addLoading"
        >确定</el-button>
      </div>
 
    </el-dialog>
 
    <!-- 新增窗口 -->
    <el-dialog
      title="新增"
      :visible.sync="addFormVisible"
    >
      <el-form
        :model="addForm"
        ref="addForm"
      >
        <el-form-item
          label="姓名"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="addForm.name"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="当前对应关系"
          :label-width="formLabelWidth"
        >
          <template>
            <el-select v-model="value" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          <el-input
            v-model="addForm.relationship"
            autocomplete="off"
          ></el-input>

          </template>
        </el-form-item>

      </el-form>
      <div>
        <el-button @click="cancel()">取消</el-button>
        <el-button
          type="primary"
          @click="sumbitAddRow()"
          :loading="addLoading"
        >确定</el-button>
      </div>
 
    </el-dialog>
 
  </div>
</template>
<script>
import storage from '../model/storage'
var _index; //定义一个全局变量，以获取行数据的行号
export default {
  data() {
    return {
      checked: true,
      tableform: [],
      formLabelWidth: '120px',
      editFormVisible: false,//是否显示编辑窗口
      editForm: [],
      addFormVisible: false,//是否显示新增窗口
      addLoading: false,
      addForm: [],
      radio: 1,
      //日期
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now();
        }
      },
      //搜索
      searchData: '',
    options: [{
              value: '选项1',
              label: '站外链接'
            }, {
              value: '选项2',
              label: '站内跳转'
            }],
            value: ''
    }
  },
  methods: {
    //获取数据
    getData() {
      var url = 'static/table.json';
      this.$http.get(url).then((data) => {
        console.log(data)
        this.tableform = data.body;
      }).catch((err) => {
        console.log(err)
      })
    },
 
    //编辑数据
    handleEdit(index, row) {
      this.editFormVisible = true;
      this.editForm = Object.assign({}, row);
      _index = index;
      // console.log(index)
      // console.log(_index)
      //取到这一栏的值，也就是明白是在那一栏进行操作，从而将编辑后的数据存到表格中
    },
    //保存编辑
    sumbitEditRow() {
      var editData = _index;
      this.tableform[editData].name = this.editForm.name;
      this.tableform[editData].relationship = this.editForm.relationship;
      this.editFormVisible = false;
    },
 
    //新增数据
    addRow() {
      this.addFormVisible = true;
      this.addForm = {
        name: '',
        radio: 1,
        relationship: ''
      }
    },
    //将新增的数据添加到表格中
    sumbitAddRow() {
      this.tableform = this.tableform || []
      this.tableform.push({
        name: this.addForm.name,
        relationship: this.addForm.relationship,
      })
      storage.set('url', this.url);
      this.addFormVisible = false
    },
    cancel() {
      this.addFormVisible = false;
      this.editFormVisible = false;
    },
    //删除数据
    handleDelete(index, row) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.tableform.splice(index, 1)
        storage.set('tableform', this.tableform)
        this.$message({
          type: 'success',
          message: '删除成功!',
        })
      }).catch((err) => {
        this.$message({
          type: 'error',
          message: err
        })
      })
    },
  },
  mounted() {
    this.getData();
  }
}
</script>
<style>
.table-grid-content {
  border-radius: 4px;
  height: 50 px;
  background: #ebeef5;
  padding: 10px;
}
.table-input {
  outline-style: none;
  border: 1px solid #ccc;
  border-radius: 3px;
  width: 200px;
  font-size: 14px;
  height: 30px;
}
.Table-table {
  padding: 5px;
}
.el-table td {
  padding: 0.01 0px;
}
.el-table th {
  padding: 0px;
}
</style>
